package pikes.peak;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import pikes.html.xhtml.table.TD;
import pikes.html.xhtml.table.TR;
import pikes.html.xhtml.table.TableCell;
import pikes.peak.CellCollection;
import pikes.peak.RequestProperty;
import pikes.peak.RowDecorator;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;

import org.springframework.web.servlet.support.RequestContext;

public class CellCollectionTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simpleCellCollection() throws Exception {
		CellCollection cellCollection = new CellCollection();

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleCellCollectionWithEmptyCollection() throws Exception {
		CellCollection cellCollection = new CellCollection(new ArrayList<RequestProperty<TableCell>>());

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleCellCollectionWithNullCellProvider() throws Exception {
		List<RequestProperty<TableCell>> cellProviders = new ArrayList<RequestProperty<TableCell>>();
		cellProviders.add(new RequestProperty<TableCell>() {

			public TableCell getValue(Map model, RequestContext requestContext) throws Exception {
				return null;
			}

		});

		CellCollection cellCollection = new CellCollection(cellProviders);

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleCellCollectionWithOneCellProvider() throws Exception {
		List<RequestProperty<TableCell>> cellProviders = new ArrayList<RequestProperty<TableCell>>();
		cellProviders.add(new RequestProperty<TableCell>() {

			public TableCell getValue(Map model, RequestContext requestContext) throws Exception {
				return new TD("boo");
			}

		});

		CellCollection cellCollection = new CellCollection(cellProviders);

		TR expected = new TR(new TD("boo"));

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleCellCollectionWithAddingOneCellProvider() throws Exception {
		CellCollection cellCollection = new CellCollection();
		cellCollection.add(new RequestProperty<TableCell>() {

			public TableCell getValue(Map model, RequestContext requestContext) throws Exception {
				return new TD("boo");
			}

		});

		TR expected = new TR(new TD("boo"));

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleCellCollectionWithAddingOneCellProviderAndRowDecorator() throws Exception {
		CellCollection cellCollection = new CellCollection(new RowDecorator() {

			public void decorateRow(TR tr, Map model, RequestContext requestContext) throws Exception {
				tr.setClazz("rowClass");
			}
		});
		cellCollection.add(new RequestProperty<TableCell>() {

			public TableCell getValue(Map model, RequestContext requestContext) throws Exception {
				return new TD("boo");
			}

		});

		TR expected = new TR(new TD("boo"));
		expected.setClazz("rowClass");

		TR actual = cellCollection.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}
}
